package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.Dorm;
import com.gdkf.dormitory.entity.Emp;
import com.gdkf.dormitory.entity.Maintenance;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.service.MaintenanceService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class MaintenanceController {
    @Autowired
    private MaintenanceService maintenanceService;

    @PostMapping("/maintenance/getAllMaintenance")
    public Msg getAllMaintenance(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Maintenance> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        maintenanceService.page(page, null);
        List<Maintenance> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("maintenanceList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/maintenance/getOneMaintenance")
    public Msg getOneMaintenance(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToMaintenance(myPage);
            Page<Maintenance> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Maintenance> wrapper = new QueryWrapper<>();
            wrapper.eq("dorm_id",((Maintenance)myPage2.getObject()).getDormId());
            wrapper.eq("hous_id",((Maintenance)myPage2.getObject()).getHousId());
            Page<Maintenance> page1 = maintenanceService.page(page, wrapper);
            List<Maintenance> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("maintenanceList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/maintenance/addMaintenance")
    public Msg addMaintenance(@RequestBody Maintenance maintenance){
        return maintenanceService.save(maintenance)?Msg.success():Msg.fail();
    }

    @PostMapping("/maintenance/deleteMaintenance")
    public Msg deleteMaintenance(@RequestBody Maintenance maintenance){
        QueryWrapper<Maintenance> wrapper = new QueryWrapper<>();
        wrapper.eq("main_id",maintenance.getMainId());
        wrapper.eq("dorm_id",maintenance.getDormId());
        wrapper.eq("hous_id",maintenance.getHousId());
        boolean remove = maintenanceService.remove(wrapper);
        return remove?Msg.success():Msg.fail();
    }

    @PostMapping("/maintenance/updateMaintenance")
    public Msg updateMaintenance(@RequestBody Maintenance maintenance){
        QueryWrapper<Maintenance> wrapper = new QueryWrapper<>();
        wrapper.eq("dorm_id",maintenance.getDormId());
        wrapper.eq("hous_id",maintenance.getHousId());
        return maintenanceService.update(maintenance,wrapper)?Msg.success():Msg.fail();
    }
}

